package com.example.wisdomclassroomapi.facade.security;

import com.example.wisdomclassroomapi.common.dto.ResponseDto;
import com.example.wisdomclassroomapi.common.enums.ExceptionInfoEnum;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class AuthenticationFailureHandlerImpl implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        response.getWriter().println(ResponseDto.fail(ExceptionInfoEnum.AM_USER_FAIL_TO_AUTHENTICATE));
    }
}
